package com.boot.main.service.impl;

import com.boot.main.entity.CcomUser;
import com.boot.main.entity.ScomOrder;
import com.boot.main.entity.ScomOrderProduct;
import com.boot.main.entity.ScomShop;
import com.boot.main.mapper.CcomUserMapper;
import com.boot.main.mapper.ScomOrderMapper;
import com.boot.main.mapper.ScomOrderProductMapper;
import com.boot.main.mapper.ScomShopMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IOrderService;
import com.boot.main.utils.IsNotEmptyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class OrderService implements IOrderService {
    @Resource
    private ScomOrderMapper orderMapper;

    @Resource
    private CcomUserMapper userMapper;

    @Resource
    private ScomShopMapper shopMapper;

    @Resource
    private ScomOrderProductMapper orderProductMapper;

    public Long getUserId(){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals =  subject.getPrincipals();
        String username = (String) principals.getPrimaryPrincipal();
        CcomUser user = userMapper.getUserByUserName(username);
        Long id = user.getId();
        return id;
    }

    @Override
    public ResponseData getOrders(Integer page, Integer limit, Integer status){
        Long userId = getUserId();
        List<ScomOrder> items = orderMapper.getOrders((page-1)*limit, limit, userId, status);
        List<ScomShop> shops = shopMapper.getShops((page-1)*limit, limit, "", userId);
        Integer count = orderMapper.getTotalCount(userId, status);
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取订单成功", map);
    }

    @Override
    public ResponseData getShopOrders(Integer page, Integer limit, Integer status){
        Long userId = getUserId();
        List<ScomShop> shops = shopMapper.getShops((page-1)*limit, limit, "", userId);
        List<ScomOrder> items = orderMapper.getShopOrders((page-1)*limit, limit, shops.get(0).getId(), status);
        Integer count = orderMapper.getTotalShopCount(shops.get(0).getId(), status);
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取订单成功", map);
    }

    @Override
    @Transactional
    public ResponseData saveOrder(ScomOrder order){
        order.setCreate_time(new Date().getTime());
        String uuid = UUID.randomUUID().toString().replace("-", "");
        order.setOrder_sn(uuid);
        order.setUser_id(getUserId());
        Integer num = orderMapper.saveOrder(order);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("保存订单失败", null);
        }else{
            Long orderId = orderMapper.getOrderIdByOrderSn(uuid);
            ScomOrderProduct orderProduct = new ScomOrderProduct();
            orderProduct.setProduct_id(order.getProduct_id());
            orderProduct.setPay_sn(order.getPay_sn());
            orderProduct.setOrder_id(orderId);
            orderProduct.setName(order.getName());
            orderProduct.setPrice(order.getPrice_pay());
            orderProduct.setNum(order.getProduct_num());
            orderProduct.setCreate_time(new Date().getTime());
            orderProduct.setPhoto_s(order.getPhoto_s());
            orderProductMapper.saveOrderProduct(orderProduct);
        }
        return ResponseDataUtil.success("保存订单成功", null);
    }

    @Override
    public ResponseData changeOrderStatus(Integer status, Long id){
        Integer num = orderMapper.changeOrderStatus(status, id);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("修改订单失败", null);
        }
        return ResponseDataUtil.success("修改订单成功", null);
    }

    @Override
    public ResponseData changeOrderPostno(String post_no, Long id){
        Integer num = orderMapper.changeOrderPostno(post_no, id);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("修改订单失败", null);
        }
        return ResponseDataUtil.success("修改订单成功", null);
    }

    @Override
    public ResponseData refundOrder(Integer refund_type,String refund_remark, Long refund_time, Long id){
        Integer num = orderMapper.refundOrder(refund_type, refund_remark, refund_time, id);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("修改订单失败", null);
        }
        return ResponseDataUtil.success("修改订单成功", null);
    }

    @Override
    public ResponseData deleteOrder(Long id, Integer del){
        Integer num = orderMapper.deleteOrder(id, del);
        return ResponseDataUtil.success("删除订单成功", null);
    }
}
